package com.ruoyi.shopping.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.shopping.domain.CommodityOrder;
import com.ruoyi.shopping.service.ICommodityOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品订单Controller
 *
 * @author lfm
 * @date 2024-08-14
 */
@RestController
@RequestMapping("/shopping/commodityOrder")
public class CommodityOrderController extends BaseController {
    @Autowired
    private ICommodityOrderService commodityOrderService;

    /**
     * 查询商品订单列表
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodityOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(CommodityOrder commodityOrder) {
        startPage();
        List<CommodityOrder> list = commodityOrderService.selectCommodityOrderList(commodityOrder);
        return getDataTable(list);
    }

    /**
     * 导出商品订单列表
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodityOrder:export')")
    @Log(title = "商品订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommodityOrder commodityOrder) {
        List<CommodityOrder> list = commodityOrderService.selectCommodityOrderList(commodityOrder);
        ExcelUtil<CommodityOrder> util = new ExcelUtil<CommodityOrder>(CommodityOrder.class);
        util.exportExcel(response, list, "商品订单数据");
    }

    /**
     * 获取商品订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodityOrder:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(commodityOrderService.selectCommodityOrderById(id));
    }

    /**
     * 新增商品订单
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodityOrder:add')")
    @Log(title = "商品订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CommodityOrder commodityOrder) {
        commodityOrderService.insertCommodityOrder(commodityOrder);
        return success(commodityOrder.getId());
    }

    /**
     * 修改商品订单
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodityOrder:edit')")
    @Log(title = "商品订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CommodityOrder commodityOrder) {
        System.out.println("commodityOrder = sdad" + commodityOrder);
        return toAjax(commodityOrderService.updateCommodityOrder(commodityOrder));
    }

    /**
     * 删除商品订单
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodityOrder:remove')")
    @Log(title = "商品订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(commodityOrderService.deleteCommodityOrderByIds(ids));
    }

    /**
     * 获取全部商品订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodityOrder:query')")
    @GetMapping(value = "/getAllOrder")
    public AjaxResult getAllOrder(Long cargoId) {
        return success(commodityOrderService.selectCommodityOrderBycargoUserId(cargoId));
    }
    /**
     * 获取商品订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodityOrder:query')")
    @GetMapping(value = "/getdetailOrder")
    public AjaxResult getdetailOrder(Long id) {
        return success(commodityOrderService.selectCommodityAllById(id));
    }
    /**
     * 获取未支付的订单的剩余时间
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodityOrder:query')")
    @GetMapping(value = "/remainder")
    public AjaxResult remainder(Long id) {
        return success(commodityOrderService.remainder(id));
    }
}
